package com.hendraanggrian.appcompat.widget;

import ohos.agp.components.TextFilter;
import ohos.agp.utils.TextTool;
import ohos.app.Context;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

/**
 * An {@link ArrayProvider} customized with filter to display items.
 * It is a direct parent of default {@link HashtagArrayAdapter} and {@link MentionArrayAdapter},
 * which are optional adapters.
 */
public abstract class SocialArrayAdapter<T> extends ArrayProvider<T> {
    private TextFilter filter;
    private List<T> tempItems = new ArrayList<>();

    public SocialArrayAdapter(Context context, int resource) {
        super(context, resource);
    }

    @Override
    public void add(T object) {
        super.add(object);
        tempItems.add(object);
    }

    @Override
    public void addAll(Collection<? extends T> collection) {
        super.addAll(collection);
        tempItems.addAll(collection);
    }

    @SafeVarargs
    @Override
    public final void addAll(T... items) {
        super.addAll(items);
        Collections.addAll(tempItems, items);
    }

    @Override
    public void remove(T object) {
        super.remove(object);
        tempItems.remove(object);
    }

    @Override
    public void clear() {
        super.clear();
        tempItems.clear();
    }

    private CharSequence convertToString(T object) {
        return object.toString();
    }

    @Override
    public TextFilter getFilter() {
        if (filter == null) {
            filter = new SocialFilter();
        }
        return filter;
    }

    private class SocialFilter extends TextFilter {
        public CharSequence convertResultToString(Object resultValue) {
            return convertToString((T) resultValue);
        }

        @Override
        protected FilterResults executeFiltering(CharSequence constraint) {
            final FilterResults results = new FilterResults();
            if (TextTool.isNullOrEmpty(constraint)) {
                return results;
            }
            final List<T> filteredItems = new ArrayList<>();
            for (final T item : tempItems) {
                if (convertResultToString(item)
                        .toString()
                        .toLowerCase(Locale.getDefault())
                        .contains(constraint.toString().toLowerCase(Locale.getDefault()))) {
                    filteredItems.add(item);
                }
            }
            results.results = filteredItems;
            results.size = filteredItems.size();
            return results;
        }

        @Override
        protected void publishFilterResults(CharSequence charSequence, FilterResults results) {
            if (results.results instanceof List) {
                final List<T> list = (List<T>) results.results;
                if (results.size > 0) {
                    SocialArrayAdapter.super.clear();
                    for (final T object : list) {
                        SocialArrayAdapter.super.add(object);
                    }
                    notifyDataChanged();
                }
            }
        }
    }
}